#include "CPrintBitmapToScreen.h"

#include "Wrappers/CSGD_TextureManager.h"
CPrintBitmapFontToScreen* CPrintBitmapFontToScreen::instance = NULL;

CPrintBitmapFontToScreen::CPrintBitmapFontToScreen(void)
{
	m_cStartChar = ' ';

	//m_nCharWidth = 32;
	//m_nCharHeight = 32;
	m_nCharWidth = 20;
	m_nCharHeight = 24;
	m_nNumCols = 10;

	//m_nImageID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/graphics/SGD_font.png");
	m_nImageID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/graphics/SGD_BMFont.png");

}

CPrintBitmapFontToScreen::~CPrintBitmapFontToScreen(void)
{
	
}

CPrintBitmapFontToScreen* CPrintBitmapFontToScreen::GetInstance(void)
{
	//if(instance == NULL)
	//	instance = new CPrintBitmapFontToScreen;

	static CPrintBitmapFontToScreen instance;

	return &instance;
}

RECT	CPrintBitmapFontToScreen::CellAlgorithm(int nID)
{
	RECT rRect;
	rRect.left = (nID%m_nNumCols)*m_nCharWidth;
	rRect.top = (nID/m_nNumCols)*m_nCharHeight;
	rRect.right = rRect.left + m_nCharWidth;
	rRect.bottom = rRect.top + m_nCharHeight;

	return rRect;
}

void CPrintBitmapFontToScreen::Print(const char* szTextToPrint, int nPosX, int nPosY, float fScale, DWORD dwColor)
{
	int nOrigX = nPosX;		

	// Loop through each char in the string

	// cache the length of the string
	int len = strlen(szTextToPrint);

	for(int i=0; i < len; i++)
	{
		char ch = szTextToPrint[i];

		if(ch == ' ')
		{
			nPosX += int((m_nCharWidth*0.7)*fScale); // multiply char width by fractional value to adjust kerning
			continue;
		}
		else if(ch == '\n')
		{
			nPosX = nOrigX;
			nPosY += int(m_nCharHeight*fScale);
			continue;
		}
		
		int id = (int)(ch - m_cStartChar);

		RECT rLetter = CellAlgorithm(id);

		// Draw it
		CSGD_TextureManager::GetInstance()->Draw(m_nImageID, nPosX, nPosY, fScale, fScale, &rLetter, 0, 0, 0, dwColor);

		nPosX += int((m_nCharWidth*0.7)*fScale); //  multiply char width by fractional value to adjust kerning
	}
}